Public Class XmlHelper
    Public ReadOnly Node As Xml.XmlNode

    Public Sub New(ByVal node As Xml.XmlNode)
        Me.Node = node
    End Sub

    Public ReadOnly Property Text() As String
        Get
            Return Node.InnerText
        End Get
    End Property

    Public ReadOnly Property ChildNodeText(ByVal childNodeName As String, Optional ByVal DefaultValue As String = "") As String
        Get
            Dim child As Xml.XmlNode = Node.SelectSingleNode(childNodeName)
            If child Is Nothing Then
                Return DefaultValue
            Else
                Return child.InnerText
            End If
        End Get
    End Property


    Public ReadOnly Property Attribute(ByVal AttributeName As String, Optional ByVal DefaultValue As String = "") As String
        Get
            Dim Attr As Xml.XmlAttribute = Node.Attributes(AttributeName)
            If Attr Is Nothing Then
                Return DefaultValue
            Else
                Return Attr.Value
            End If
        End Get
    End Property

    Public Function AttributeToEnum(Of T)(ByVal AttributeName As String) As Integer
        Dim enumType As System.Type = GetType(T)
        Dim ConfigAttrValue As String = Attribute(AttributeName, "")
        If ConfigAttrValue.Equals("") Then
            Return CType(System.Enum.GetValues(enumType).GetValue(0), Integer)
        Else
            Try
                Return CType(System.Enum.Parse(enumType, ConfigAttrValue, True), Integer)
            Catch ex As Exception
                Return CType(System.Enum.GetValues(enumType).GetValue(0), Integer)
            End Try
        End If
    End Function

    Public ReadOnly Property AttributeSpecialString(ByVal AttributeName As String, Optional ByVal DefaultValue As String = "") As String
        Get
            Dim Attr As Xml.XmlAttribute = Node.Attributes(AttributeName)
            If Attr Is Nothing Then
                Return DefaultValue
            Else
                Dim output As String = Attr.Value
                output = output.Replace("tab", vbTab)
                output = output.Replace("pipe", "|")
                output = output.Replace("comma", ",")
                Return output
            End If
        End Get
    End Property

    Public ReadOnly Property BoolAttribute(ByVal AttributeName As String, Optional ByVal DefaultValue As Boolean = True) As Boolean
        Get
            Dim Attr As Xml.XmlAttribute = Node.Attributes(AttributeName)
            If Attr Is Nothing Then
                Return DefaultValue
            Else
                Select Case Attr.Value.ToUpper
                    Case "NO", "N", "NIE", "0"
                        Return False
                    Case "YES", "Y", "TAK", "TAK", "1"
                        Return True
                    Case Else
                        Return DefaultValue
                End Select
            End If
        End Get
    End Property
End Class
